package org.devocean.groupbuying.web.frontend;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.devocean.groupbuying.domain.Promotion;
import org.devocean.groupbuying.service.ProductService;
import org.devocean.groupbuying.service.PromotionService;
import org.devocean.groupbuying.web.frontend.displaybeans.PromotionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = { "/{lang:[a-z]{2}}/promotions" })
// @RequestMapping(value={"", "/{lang:[a-z]{2}}"})
public class PromotionController {

	@Autowired
	private PromotionService promotionService;
	
	@Autowired
	private ProductService productService;

	@RequestMapping("{id}")
	public String explorerDetails(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("id") Integer id) {

		map.put("promotionBean",
				new PromotionBean(promotionService.getPromotionById(id)));
		map.put("from", "promotions/" + id);
		
		return "promotion";
	}

	@RequestMapping("/product/{id}")
	public String byProduct(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("id") Integer productID) {
		Set<PromotionBean> c = new HashSet<PromotionBean>();

		for (Promotion pr : promotionService.getPromotionsByProduct(productID)) {
			c.add(new PromotionBean(pr));
		}
		map.put("promotionBeans", c);
		map.put("productTitle", productService.getProductById(productID)
				.getArticlesLocalization().getArticles().iterator().next()
				.getTitle());
		return "promotionsByProd";
	}
}
